/* 
 * This trigger assigns a value to the color__c custom field before opportunity
 * line items are saved to the database.
 */
trigger oppLineTrigger on OpportunityLineItem bulk (before insert) {
	//Determine the distinct pricebook entries
	Set<Id> pbeIds = new Set<Id>();
	for (OpportunityLineItem oli : Trigger.new) 
		pbeIds.add(oli.pricebookentryid);
		
	//Query the pricebook entries
	Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([select product2.color__c from pricebookentry
where id in :pbeIds]);

	// Now set the colors on the opportunity line items
	for (OpportunityLineItem oli : Trigger.new)
		oli.color__c = entries.get(oli.pricebookEntryId).product2.color__c;
}